package com.doit.demo01;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test_condition {
    public static void main(String[] args) throws InterruptedException {
        Lock l = new ReentrantLock();
        Condition c1 = l.newCondition();
        Condition c2 = l.newCondition();


        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("开始等待");
                l.lock();
                try {
                    c1.await();
                    c2.await();
                    System.out.println("已经唤醒");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                l.unlock();

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("对对子");
                l.lock();
                try {
                    c2.await();
                    System.out.println("最强大脑");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                l.unlock();
            }
        }).start();

        Thread.sleep(2000);
        l.lock();
        System.out.println("准备唤醒");
        c1.signal();
        l.unlock();
        l.lock();
//        c2.signal();

    }
}
